package com.scrcu.core.batch.action;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;
import com.scrcu.core.batch.biz.BatchExecutionBiz;
import com.scrcu.core.batch.biz.BatchInfoBiz;
import com.scrcu.core.batch.constant.BatchConstants;
import com.scrcu.core.batch.constant.Constants;
import com.scrcu.core.batch.entity.BatchExecution;
import com.scrcu.core.batch.entity.BatchInfo;
import com.scrcu.core.batch.service.BatchDispatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/execution")
public class BatchExecutionAction {

    @Autowired
    private BatchExecutionBiz batchExeBiz;
    @Autowired
    private BatchInfoBiz batchInfoBiz;

    @Autowired
    private ApplicationContext appCtx;

    private ReplyInfo<Object> getBatchDispatcher(String id) {
        BatchExecution be = batchExeBiz.get(id);
        if (be == null) {
            return new ReplyInfo<>(BatchConstants.EXECUTION_ERROR_NOT_FOUND);
        }
        BatchInfo bi = null;
        if (Util.strIsNotEmpty(be.getInfoId())) {
            bi = batchInfoBiz.get(be.getInfoId());
        }
        if (bi == null) {
            return new ReplyInfo<>(BatchConstants.EXECUTION_ERROR_MISSING_INFO);
        }
        BatchDispatcher bd = null;
        if (Util.strIsNotEmpty(bi.getImpl())) {
            if (appCtx.containsBean(bi.getImpl())) {
                bd = (BatchDispatcher) appCtx.getBean(bi.getImpl());
            }
        }
        if (bd == null) {
            return new ReplyInfo<>(Constants.WARN_INVALID, "impl");
        }
        return new ReplyInfo<>(true, bd);
    }

    /**
     * 续跑
     * @param id
     * @return
     */
    @RequestMapping("/resume.do")
    @Authorized(code = "batch.execution.resume")
    public ReplyInfo<Object> resume(String id) {
        if (Util.strIsEmpty(id)) {
            return new ReplyInfo<>(Constants.WARN_INVALID, "resume");
        }
        ReplyInfo<Object> bd = this.getBatchDispatcher(id);
        if (bd != null) {
            int result = ((BatchDispatcher) bd.getData()).resume(id);
            return new ReplyInfo<>(true, result);
        }
        return null;
    }

    /**
     * 重跑
     * @param id
     * @return
     */
    @RequestMapping("/redo.do")
    @Authorized(code = "batch.execution.redo")
    public ReplyInfo<Object> redo(String id) {
        if (Util.strIsEmpty(id)) {
            return new ReplyInfo<>(Constants.WARN_INVALID, "redo");
        }
        ReplyInfo<Object> bd = this.getBatchDispatcher(id);
        if (bd != null) {
            int result = ((BatchDispatcher) bd.getData()).redo(id);
            return new ReplyInfo<>(true, result);
        }
        return null;
    }

    /**
     * 取消
     * @param id
     * @param force
     * @return
     */
    @RequestMapping("/cancel.do")
    @Authorized(code = "batch.execution.cancel")
    public ReplyInfo<Object> cancel(String id, boolean force) {
        ReplyInfo<Object> bd = this.getBatchDispatcher(id);
        if (bd != null && bd.getData() instanceof BatchDispatcher) {
            int result = ((BatchDispatcher) bd.getData()).cancel(id, force);
            return new ReplyInfo<>(true, result);
        }
        return ReplyInfo.FAILED;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete.do")
    @Authorized(code = "batch.execution.delete")
    public ReplyInfo<Object> delete(String id) {
        batchExeBiz.delete(id);
        return ReplyInfo.SUCCESS;
    }

    /**
     * 查询列表
     * @param infoId
     * @param key
     * @param status
     * @param startTime
     * @param endTime
     * @param orderBy
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/list.do")
    @Authorized(code = "batch.execution.list")
    public ReplyInfo<Object> list(String infoId, String key, Short status, String startTime, String endTime, String orderBy, int pageNo, int pageSize) {
        PagedResult<BatchExecution> pr = batchExeBiz.list(infoId, startTime, endTime, pageNo, pageSize);
        return new ReplyInfo<>(true, JsonUtil.from(pr));
    }

    /**
     * 获取
     * @param id
     * @return
     */
    @RequestMapping("/get.do")
    @Authorized(code = "batch.execution.get")
    public ReplyInfo<Object> get(String id) {
        if (Util.strIsEmpty(id)) {
            return new ReplyInfo<>(Constants.WARN_INVALID, "get");
        }
        BatchExecution o = batchExeBiz.get(id);
        return new ReplyInfo<>(true, JsonUtil.from(o));
    }

    /**
     * 根据ID数组查询
     * @param keys
     * @return
     */
    @RequestMapping("/refresh.do")
    @Authorized(code = "batch.execution.list")
    public ReplyInfo<Object> refresh(String keys) {
        String[] ids = keys.trim().split(",");
        List<BatchExecution> list = batchExeBiz.list(ids);
        return new ReplyInfo<>(true, JsonUtil.from(list));
    }

    /**
     * 查询执行中的任务
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/executing.do")
    @Authorized(code = "batch.execution.list")
    public ReplyInfo<Object> listExecuting(int pageNo, int pageSize) {
        PagedResult<Map<String, Object>> oPr = batchExeBiz.listExecuting(pageNo, pageSize);
        return new ReplyInfo<>(true, JsonUtil.from(oPr));
    }
}